﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using USERS.CYT369.COM.Models;

namespace USERS.CYT369.COM.Controllers
{
    public partial class BrandController
    {

        #region Brand
        [HttpPost]
        public IActionResult SaveBrand([FromBody] USERS.CYT369.COM.MODELS.Brand.Brand brand)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            brand.EditorID = this.GetUserId();
            brand.EditorName = this.UserSession.NickName;
            brand.EditorFullName = this.UserSession.FullName;
            brand.ReviewResult = -2;
            if (string.IsNullOrEmpty(brand.Name))
            {
                brand.Name = "[未命名]";
            }

            if (USERS.CYT369.COM.DAL.BrandService.Instance.SaveBrand(brand))
            {
                retVal.Result = true;
                retVal.Data = brand;
                retVal.Msg = "success";
            }

            return DefaultJson(retVal);
        }

        [HttpPost]
        public IActionResult GetBrandByID(long id)
        {
            var brand = USERS.CYT369.COM.DAL.BrandService.Instance.GetBrandByID(id);
            return DefaultJson(brand);
        }

        [HttpPost]
        public IActionResult SubmitAudit(long id)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            retVal.Result = USERS.CYT369.COM.DAL.BrandService.Instance.SubmitAudit(id, this.GetUserId());

            return DefaultJson(retVal);
        }

        [HttpDelete]
        public IActionResult BrandDelete(string id)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();

            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                long? docId = argsJson.GetInteger("ID");
                if (docId > 0)
                {
                    retVal.Result = USERS.CYT369.COM.DAL.BrandService.Instance.BrandDelete(docId.Value);
                }
            }
            return DefaultJson(retVal);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult BrandImageDelete(long brandId, long fileId)
        {
            var retVal = new USERS.CYT369.COM.Models.ExecuteResult();
            
            retVal.Result = USERS.CYT369.COM.DAL.BrandService.Instance.BrandImageDelete(brandId, fileId, this.GetUserId());

            return DefaultJson(retVal);
        }

        #endregion

        #region BrandType
        public IActionResult CategorysGetByParentID(int id)
        {
            var documentTypes = USERS.CYT369.COM.DAL.BrandService.Instance.CategorysGetByParentID(id);

            return DefaultJson(documentTypes);
        }
        #endregion




    }
}
